<?php


namespace app\api\controller;

use app\admin\model\Thumbs;
use app\common\controller\Api;
use app\admin\model\Playlet\Playlet as PlayletModel;
use app\admin\model\Playlet\PlayletChapter as PlayletListsModel;
use app\common\model\Category;
use think\Request;

class Playlet extends Api
{
    protected $noNeedLogin = ['*'];

    /** 短剧列表
     * @param Request $request
     */
    public function list(\think\Request $request)
    {
        if (!$request->isPost()) {
            $this->error('ＤＯＮ＇Ｔ　ＧＥＴ');
        }
        $req = $request->post();
        $where = ['status' => 'normal'];
        $class = $req['class'];
        if ($class == 1) {
            $where['ishot'] = 1;
        } else {
            $where['category'] = $class;
        }

        $res = PlayletModel::where($where)->orderRaw('id desc')->field('id,cover,name,thumbs,sum')->paginate($req['every'], false, ['var_page' => 'current']);

        if ($this->auth->isLogin()) {
            $user = $this->auth->getUserinfo();

            foreach ($res as &$value) {
                //点赞
                $n = Thumbs::where(['userid' => $user['id'], 'thumbsid' => $value['id'], 'class' => 3])->count();
                if ($n) {
                    $value['give'] = 1;
                } else {
                    $value['give'] = 0;
                }

            }
        }
        $this->result('短剧', $res, 200);
    }

   public function getInfo()
   {
       $where['id'] = $this->request->param('playlet_id');
       $playlet= PlayletModel::where($where)->field('id,cover,name,thumbs,sum')->find();
       $this->result('短剧', $playlet, 200);
   }

    public function category(\think\Request $request)
    {
        $list = Category::where('type', 'category')->field('name,id')->select();

        $this->success('成功', $list);
    }

}
